var express = require('express')
var router = express.Router()

let dormitoryData = [
    {
        id: '1', buildingId: '1栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '11', buildingId: '1栋', storey: '一楼', dormId: '1-102', level: 'L2' },
            { id: '12', buildingId: '1栋', storey: '一楼', dormId: '1-103', level: 'L2' },
            { id: '13', buildingId: '1栋', storey: '一楼', dormId: '1-104', level: 'L2' },
            { id: '14', buildingId: '1栋', storey: '一楼', dormId: '1-105', level: 'L2' },
        ]
    },
    {
        id: '2', buildingId: '2栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '21', buildingId: '2栋', storey: '一楼', dormId: '2-102', level: 'L2' },
            { id: '22', buildingId: '2栋', storey: '一楼', dormId: '2-103', level: 'L2' },
            { id: '23', buildingId: '2栋', storey: '一楼', dormId: '2-104', level: 'L2' },
            { id: '24', buildingId: '2栋', storey: '一楼', dormId: '2-105', level: 'L2' },
        ]
    },
    {
        id: '3', buildingId: '3栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '31', buildingId: '3栋', storey: '一楼', dormId: '3-102', level: 'L2' },
            { id: '32', buildingId: '3栋', storey: '一楼', dormId: '3-103', level: 'L2' },
            { id: '33', buildingId: '3栋', storey: '一楼', dormId: '3-104', level: 'L2' },
            { id: '34', buildingId: '3栋', storey: '一楼', dormId: '3-105', level: 'L2' },
        ]
    },
    {
        id: '4', buildingId: '4栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '41', buildingId: '4栋', storey: '一楼', dormId: '4-102', level: 'L2' },
            { id: '42', buildingId: '4栋', storey: '一楼', dormId: '4-103', level: 'L2' },
            { id: '43', buildingId: '4栋', storey: '一楼', dormId: '4-104', level: 'L2' },
            { id: '44', buildingId: '4栋', storey: '一楼', dormId: '4-105', level: 'L2' },
        ]
    },
    {
        id: '5', buildingId: '5栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '51', buildingId: '5栋', storey: '一楼', dormId: '5-102', level: 'L2' },
            { id: '52', buildingId: '5栋', storey: '一楼', dormId: '5-103', level: 'L2' },
            { id: '53', buildingId: '5栋', storey: '一楼', dormId: '5-104', level: 'L2' },
            { id: '54', buildingId: '5栋', storey: '一楼', dormId: '5-105', level: 'L2' },
        ]
    },
    {
        id: '6', buildingId: '6栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '61', buildingId: '6栋', storey: '一楼', dormId: '6-102', level: 'L2' },
            { id: '62', buildingId: '6栋', storey: '一楼', dormId: '6-103', level: 'L2' },
            { id: '63', buildingId: '6栋', storey: '一楼', dormId: '6-104', level: 'L2' },
            { id: '64', buildingId: '6栋', storey: '一楼', dormId: '6-105', level: 'L2' },
        ]
    },
    {
        id: '7', buildingId: '7栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '71', buildingId: '7栋', storey: '一楼', dormId: '7-102', level: 'L2' },
            { id: '72', buildingId: '7栋', storey: '一楼', dormId: '7-103', level: 'L2' },
            { id: '73', buildingId: '7栋', storey: '一楼', dormId: '7-104', level: 'L2' },
            { id: '74', buildingId: '7栋', storey: '一楼', dormId: '7-105', level: 'L2' },
        ]
    },
    {
        id: '8', buildingId: '8栋', storey: '', dormId: '', level: 'L1', children: [
            { id: '81', buildingId: '8栋', storey: '一楼', dormId: '8-102', level: 'L2' },
            { id: '82', buildingId: '8栋', storey: '一楼', dormId: '8-103', level: 'L2' },
            { id: '83', buildingId: '8栋', storey: '一楼', dormId: '8-104', level: 'L2' },
            { id: '84', buildingId: '8栋', storey: '一楼', dormId: '8-105', level: 'L2' },
        ]
    },
]

router.get('/l1', function (req, res, next) {
    res.send(dormitoryData)
})

router.get('/list', function (req, res, next) {
    if (!req.query.pageIndex) {
        res.send('缺少pageIndex')
    }

    if (!req.query.pageSize) {
        res.send('缺少pageSize')
    }

    let data = dormitoryData.filter(item => {
        return item.dormId.includes(req.query.dormId)
    })

    let start = (Number(req.query.pageIndex) - 1) * req.query.pageSize
    let end = Number(req.query.pageIndex) * req.query.pageSize

    res.send({
        pageIndex: req.query.pageIndex,
        pageSize: req.query.pageSize,
        total: data.length,
        data: data.slice(start, end)
    })
})


router.post('/add', function (req, res, next) {

    let data = {
        id: Math.ceil(new Date().getTime() + Math.random() * 10000),
        buildingId: req.body.buildingId,
        storey: req.body.storey,
        dormId: req.body.dormId,
        level: req.body.level
    }

    let index = dormitoryData.findIndex(item => {
        return Number(item.id) === Number(req.body.id)
    })

    if (index >= 0){
        if (dormitoryData[index].children) {
            dormitoryData[index].children.push(data)
        } else {
            dormitoryData[index].children = []
    
            dormitoryData[index].children.push(data)
        }
    } else {
        dormitoryData.push(data)
    }

    res.send({
        msg: '新增成功',
        code: 200,
        data: true
    })
})

router.put('/update', function (req, res, next) {
    if (!req.body.id) {
        res.send('缺少id字段')

        return
    }

    let index = dormitoryData.findIndex(item => {
        return String(item.id) === String(req.body.id)
    })

    if (index < 0) {
        res.send({
            msg: '编辑失败，未通过buildingId找到对应数据',
            code: 500,
            data: false
        })

        return
    }

    dormitoryData[index] = Object.assign({}, req.body)

    res.send({
        msg: '编辑成功',
        code: 200,
        data: true
    })
})

router.delete('/delete', function (req, res, next) {
    if (!req.query.id) {
        res.send('缺少id字段')
    }

    let index = dormitoryData.findIndex(item => {
        return Number(item.id) === Number(req.query.id)
    })

    if (index < 0) {
        res.send({
            msg: '删除失败，未能通过id找到对应数据',
            code: 500,
            data: false
        })
        return
    }

    dormitoryData.splice(index, 1)

    res.send({
        msg: '删除成功',
        code: 200,
        data: true
    })
})

module.exports = router